package com.hns.can.mainte.service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hns.can.common.constants.GlobalConst;
import com.hns.can.common.formula.FillCodeDscValue;
import com.hns.can.common.formula.FillParamsVo;
import com.hns.can.mainte.entity.AppCarMaintenInfoEntity;
import com.hns.can.mainte.entity.AppCarMaintenRmndrInfoEntity;
import com.hns.can.mainte.vo.MaintenanceParamVo;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.PubUtil;

/**
 * 维保计划
 * @author:yecb
 * @email:lps00@163.com
 * @version Revision 2.0.0
 */
@Service
public class ReportCarMaintePlanService extends CommonService {
	@Autowired
    private FillCodeDscValue codeDscValue;
	/**
	 * 查询维保计划
	 * @param conditionVo
	 * @return
	 * @throws Exception
	 * @author:yecb
	 * @email:lps00@163.com
	 */
	@SuppressWarnings({ "unchecked"})
    @Transactional
    public Pagination searchMaintenancePage(MaintenanceParamVo conditionVo) throws Exception {
		List appCarMaintenInfoResultList=searchMaintenanceList(conditionVo);
	    Pagination page = new Pagination( this.getRowBounds(), appCarMaintenInfoResultList.size());
	    page.setData(appCarMaintenInfoResultList);
        return page;
    }
	
	/**
	 * 查询维保计划列表
	 * @param conditionVo
	 * @return
	 * @throws Exception
	 * @author:yecb
	 * @email:lps00@163.com
	 */
	@SuppressWarnings({ "unchecked"})
    @Transactional
    public List searchMaintenanceList(MaintenanceParamVo conditionVo) throws Exception {
    	//1.通过维保明细查询出维保计划
		List<AppCarMaintenInfoEntity> appCarMaintenInfoList = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("reportCarMaintePlan.getMaintenPlanList", conditionVo), AppCarMaintenInfoEntity.class);
		if(PubUtil.isEmpty(appCarMaintenInfoList)){
			 throw new BusinessException(MsgType.WARN.getFlag(),"没有设置维保信息，无法查询出维保计划！");
		}
		//2.查询维保设置
		List appCarMaintenRmndrInfoList = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("reportCarMaintePlan.getMaintenRmndrInfo", conditionVo), AppCarMaintenRmndrInfoEntity.class);
		Map<String,String> mapValue = new HashMap<String,String>();
	    mapValue.put("mntType", "MAIN1000");
	    codeDscValue.fillCodeDscValue(appCarMaintenRmndrInfoList, mapValue);
		//3.获取不同车型的维保设置信息(key:车型;value:对应的维保类型列表)
		HashMap<String,ArrayList<AppCarMaintenRmndrInfoEntity>> appCarMaintenRmndrInfoMap=getCarMaintenRmndrInfoByCarTypeMap(appCarMaintenRmndrInfoList);
		List appCarMaintenInfoResultList=new ArrayList();
		//4.根据维保计划里面的车辆类型，确定该车型的维保类别，然后计算出需要的维保里程以及维保时间
		for(AppCarMaintenInfoEntity appCarMaintenInfoEntity: appCarMaintenInfoList){
			ArrayList<AppCarMaintenRmndrInfoEntity> appTypesList=appCarMaintenRmndrInfoMap.get(appCarMaintenInfoEntity.getCarType());
			if(PubUtil.isEmpty(appTypesList)||PubUtil.isEmpty(appCarMaintenInfoEntity.getTotalMile())){
				continue;
			}
			Double minItrvMile=99999999999.9;  //最小间隔里程
			AppCarMaintenRmndrInfoEntity appCarMaintenRmndrInfoResult=null;
			//4.1确定该车型的维保类别appCarMaintenRmndrInfoResult，间隔差最小值才是我们需要进行的维保类别
			for(AppCarMaintenRmndrInfoEntity appCarMaintenRmndrInfoEntity:appTypesList){
				
				Double interMile=(appCarMaintenInfoEntity.getTotalMile().doubleValue()-appCarMaintenInfoEntity.getMaintenMile().doubleValue());
				if(appCarMaintenRmndrInfoEntity.getItrvMile().doubleValue()>=interMile){
					Double minItrvMileTemp=appCarMaintenRmndrInfoEntity.getItrvMile().doubleValue()-interMile;//计算里程间隔的最小差值
					if(minItrvMileTemp<minItrvMile){
						minItrvMile=minItrvMileTemp;
						appCarMaintenRmndrInfoResult=appCarMaintenRmndrInfoEntity;
					}
				}
			}
			if(appCarMaintenRmndrInfoResult==null){
				continue;
			}
			//4.2计算出需要的维保里程以及维保时间
			appCarMaintenInfoEntity.setPlanMile(appCarMaintenInfoEntity.getMaintenMile().doubleValue()+appCarMaintenRmndrInfoResult.getItrvMile().doubleValue());//计划保养里程(上次维保里程+ 保养间隔里程)
			appCarMaintenInfoEntity.setMaintenCgy(appCarMaintenRmndrInfoResult.getMntType());//计划维保类别
			appCarMaintenInfoEntity.setPartsCode(appCarMaintenRmndrInfoResult.getPartsCode());//建议更换零部件
			Double remainMile=appCarMaintenInfoEntity.getMaintenMile().doubleValue()
					+appCarMaintenRmndrInfoResult.getItrvMile().doubleValue()
					-appCarMaintenInfoEntity.getTotalMile().doubleValue();//剩余里程(上次维保里程+间隔里程 - 总里程)
			DecimalFormat    df   = new DecimalFormat("######0.00");  
			appCarMaintenInfoEntity.setRemainMile(Double.valueOf(df.format(remainMile)));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
            Calendar cd = Calendar.getInstance();   
            cd.setTime(sdf.parse(appCarMaintenInfoEntity.getMaintenDate()));
            cd.add(Calendar.MONTH, appCarMaintenRmndrInfoResult.getItrvTime().intValue());//增加一个月
			appCarMaintenInfoEntity.setPlanMaintenDate(sdf.format(cd.getTime()));// 计算计划保养日期（车辆上次维保日期 + 间隔日期）
			Long remainTime=(cd.getTime().getTime()-new Date().getTime())/(24*60*60*1000);// 剩余天数（车辆上次维保日期 + 间隔日期 - 当前日期）
			appCarMaintenInfoEntity.setRemainTime(remainTime);
			//过滤条件,过滤剩余里程，剩余天数满足条件的
			if(!PubUtil.isEmpty(conditionVo.getRemainDay())){
				if(remainTime>conditionVo.getRemainDay()){
					continue;
				}
			}
			if(!PubUtil.isEmpty(conditionVo.getRemainMile())){
				if(remainMile>(conditionVo.getRemainMile()*1000)){
					continue;
				}
			}
			appCarMaintenInfoResultList.add(appCarMaintenInfoEntity);
		}
		Map<String,String> map = new HashMap<String,String>();
        map.put("licPltNo",  GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo vo = new FillParamsVo("carId", map);
        codeDscValue.fillInfoValue(appCarMaintenInfoResultList,new String[] {GlobalConst.CACHE_KEY_BASE_CAR_INFO}, vo);
        return appCarMaintenInfoResultList;
    }

	/**
	 * 获取不同车型的维保设置信息
	 * @param appCarMaintenRmndrInfoList 维保设置LIST
	 * @return HashMap<String,ArrayList<AppCarMaintenRmndrInfoEntity>> key:车辆类型；value:对应的设置列表
	 * @author:yecb
	 * @email:lps00@163.com
	 */
	private HashMap<String,ArrayList<AppCarMaintenRmndrInfoEntity>> getCarMaintenRmndrInfoByCarTypeMap(List<AppCarMaintenRmndrInfoEntity> appCarMaintenRmndrInfoList){
		HashMap<String,ArrayList<AppCarMaintenRmndrInfoEntity>> appCarMaintenRmndrInfoMap=new HashMap<String,ArrayList<AppCarMaintenRmndrInfoEntity>>();
        if(PubUtil.isNotEmpty(appCarMaintenRmndrInfoList)){
            for(AppCarMaintenRmndrInfoEntity appCarMaintenRmndrInfoEntity:appCarMaintenRmndrInfoList){
                ArrayList<AppCarMaintenRmndrInfoEntity> appTypesList=appCarMaintenRmndrInfoMap.get(appCarMaintenRmndrInfoEntity.getCarType());
                if(PubUtil.isEmpty(appTypesList)){
                    ArrayList<AppCarMaintenRmndrInfoEntity> appTypeList=new ArrayList<AppCarMaintenRmndrInfoEntity>();
                    appTypeList.add(appCarMaintenRmndrInfoEntity);
                    appCarMaintenRmndrInfoMap.put(appCarMaintenRmndrInfoEntity.getCarType(), appTypeList);
                }else{
                    appTypesList.add(appCarMaintenRmndrInfoEntity);
                }
            }
        }
		return appCarMaintenRmndrInfoMap;
	}
	
 
}
